package com.example.springstudy.coucrrentSummary;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.Data;

import java.util.*;

/**
 * @author zhaojunzhou
 * @date 2020/4/24 10:58
 */
public class MyCache {
    public static void main(String[] args) {
        LinkedHashMap<String, Integer> map2 = new LinkedHashMap<String, Integer>(6, 0.75f, true);
        map2.put("a", 1);
        map2.put("b", 2);
        map2.put("c", 3);
        map2.put("d", 4);
        map2.get("c");
        map2.get("b");
        map2.get("a");
        System.out.println(map2);
        final Cache<Integer, Integer> map = CacheBuilder.newBuilder().recordStats().build();// 结果可能大于1000
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 1000; i++) {
                    map.put(i, i);
                }
            }
        };
        int threadNum = 2;// 线程数
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < threadNum; i++) {
            Thread thread = new Thread(runnable);
            threadList.add(thread);
            thread.start();
        }

        // 主线程等待子线程执行完成
        for (Thread thread : threadList) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(map.size());// 结果可能大于1000
    }
}

@Data
class CacheTest {
    public static volatile Cache<String, Integer> cache2 = CacheBuilder.newBuilder().maximumSize(1000).build();
}

class CreateThreadDemo extends Thread {
    @Override
    public void run() {
        for (int a = 0; a < 30; a++) {
            String name = Thread.currentThread().getName();
            int num = CacheTest.cache2.getIfPresent("test");
            System.out.println(name + ":" + num--);
            CacheTest.cache2.put("test", num);
        }
    }
}
